package com.jvtd.demo.recyclerView;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jvtd.demo.R;
import com.jvtd.utils.SmartRefreshUtils;
import com.jvtd.widget.emptyView.JvtdEmptyView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RecyclerViewActivity extends AppCompatActivity implements JvtdEmptyView.OnClickListener, OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    private DemoRecyclerView mRecyclerView;
    private SmartRefreshLayout mRefreshLayout;
    private int cIndex = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        mRefreshLayout = findViewById(R.id.refresh_layout);
        mRecyclerView = findViewById(R.id.recyclerview);
        mRecyclerView.openEmptyView(this);
        mRecyclerView.setRefreshLayout(mRefreshLayout,this);
        mRecyclerView.setOnLoadMoreListener(this);

        TextView errorBtn = findViewById(R.id.error);
        TextView emptyBtn = findViewById(R.id.empty);
        errorBtn.setOnClickListener(view -> mRecyclerView.firstLoadFailed());
        emptyBtn.setOnClickListener(view -> mRecyclerView.setData(new ArrayList<>()));

        onRefresh(null);
    }

    @Override
    public void onClick(View view, int state) {
        switch (state) {
            case JvtdEmptyView.STATE_ERROR:
                onRefresh(null);
                break;
            case JvtdEmptyView.STATE_NORMAL:
                Toast.makeText(this,"功能键",Toast.LENGTH_SHORT).show();
                break;
            case JvtdEmptyView.STATE_LOADING:
            default:
                break;
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        new Handler().postDelayed(() -> {
            Random random = new Random();
            boolean isSuccess = random.nextBoolean();
            if (isSuccess)
                mRecyclerView.setData(getDemoList());
            else
                mRecyclerView.firstLoadFailed();
        },2000);
    }

    @SuppressLint("DefaultLocale")
    private List<RvBean> getDemoList(){
        cIndex ++;
        Random random = new Random();
        int num = Math.min(10,random.nextInt(20));
        List<RvBean> beans = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            RvBean bean = new RvBean();
            bean.setName(String.format("测试数据%d%d", cIndex * 10, i));
            beans.add(bean);
        }
        return beans;
    }

    @Override
    public void onLoadMoreRequested() {
        new Handler().postDelayed(() -> {
            Random random = new Random();
            boolean isSuccess = random.nextBoolean();
            if (isSuccess)
                mRecyclerView.loadData(getDemoList());
            else
                mRecyclerView.loadMoreFailed();
        },2000);
    }
}
